import isEmpty from 'loadsh/isEmpty'
import { SPACEID } from '/constData/requestFun/_globalData';
import { GetContractInfo, GetInfoPicture, GetPreviewImage, PostContractInfo, PostDeleteData, PostInfoPicture } from '/constData/requestFun/_httpRequest';
import { ContractManagementDeleteData, GetContractManagement, GetContractManagementById, GetTapeoutContract, GetTapeoutContractById, GetTopicContract, GetTopicContractById, PostContractManagement, PostTapeoutContract, PostTopicContract, TopicContractDeleteData } from '/constData/requestFun/_teseRequest';
import compact from 'loadsh/compact'
Page({
  data: {
    item: 1,
    index: 0,
    min: null,
    max: new Date('2033/12/02'),
    defaultDate: new Date(),
    arrIndex: 0,
    index: 0,
    TopicContractInfo: {
      T_CorpId: '',
      T_ApplicatDepart: '',
      T_TopicTitle: '',
      T_TopicContent: '',
      T_ReviewConference: '',
      T_MeetingTime: '',
      T_Participants: '武总 谢总 梁律 刘部',
      T_Annex: '[]',
      T_Notes: '',
      C_ContractNumber_A: '',//合同编号
      C_SignDate_A: '',//签约日期
      C_OurCompany_A: '', //我方单位名称
      C_OppositeCompany_A: '',//对方单位名称
      C_Content_A: '',//内容
      C_Picture_A: '[]',//图片
      C_ApprovalAnnex_A: '[]', //合同审批附件
      C_ContractNumber_B: '',//合同编号
      C_SignDate_B: '',//签约日期
      C_OurCompany_B: '', //我方单位名称
      C_OppositeCompany_B: '',//对方单位名称
      C_Content_B: '',//内容
      C_Picture_B: '[]',//图片
      C_ApprovalAnnex_B: '[]', //合同审批附件
      ContractDifferences:''//合同差价
    },
    ReceiveData: [],

    displayAnnex: [],
    displayPicture_A: [],
    displayApprovalAnnex_A: [],
    displayPicture_B: [],
    displayApprovalAnnex_B: [],


    position: '',
    animation: true,
    basicVisible: false,
    closeVisible: false,
    DisTopicData: [],
    DisTopicDisplayData: [],


    DisplayTopicContract: {
      T_CorpId: '',
      T_ApplicatDepart: '',
      T_TopicTitle: '',
      T_TopicContent: '',
      T_ReviewConference: '',
      T_MeetingTime: '',
      T_Participants: '武总 谢总 梁律 刘部',
      T_Annex: '[]',
      T_Notes: '',
    },
  },
  Delete(e) {
    const id = e.currentTarget.dataset.id
    TopicContractDeleteData(id, (res) => {
      my.alert({
        title: '撤销成功',
      });
      //刷新数据
      this.setData({
        ReceiveData: this.data.ReceiveData.filter((it) => {
          return it.item1.id !== id
        })
      })
    }, (err) => {
      my.alert({
        title: '撤销失败',
      });
    })
  },
  onApplicatDepartChange(e) {
    this.setData({
      TopicContractInfo: {
        ...this.data.TopicContractInfo,
        T_ApplicatDepart: e
      }
    })
  },
  onTopicTitleChange(e) {
    this.setData({
      TopicContractInfo: {
        ...this.data.TopicContractInfo,
        T_TopicTitle: e
      }
    })
  },
  onTopicContentChange(e) {
    this.setData({
      TopicContractInfo: {
        ...this.data.TopicContractInfo,
        T_TopicContent: e
      }
    })
  },
  onReviewConferenceChange(e) {
    this.setData({
      TopicContractInfo: {
        ...this.data.TopicContractInfo,
        T_ReviewConference: e
      }
    })
  },
  handleOk(date, format, e) {
    this.setData({
      TopicContractInfo: {
        ...this.data.TopicContractInfo,
        T_MeetingTime: date
      },
    })
  },
  onNotesChange(e) {
    this.setData({
      TopicContractInfo: {
        ...this.data.TopicContractInfo,
        T_Notes: e
      }
    })
  },


  handleOk_A(date, format, e) {
    this.setData({
      TopicContractInfo: {
        ...this.data.TopicContractInfo,
        C_SignDate_A: date
      },
    })
  },
  onContractNumberChange_A(e) {
    this.setData({
      TopicContractInfo: {
        ...this.data.TopicContractInfo,
        C_ContractNumber_A: e
      }
    })
  },
  onOurCompanyChange_A(e) {
    this.setData({
      TopicContractInfo: {
        ...this.data.TopicContractInfo,
        C_OurCompany_A: e
      }
    })
  },
  onOppositeCompanyChange_A(e) {
    this.setData({
      TopicContractInfo: {
        ...this.data.TopicContractInfo,
        C_OppositeCompany_A: e
      }
    })
  },
  onContentChange_A(e) {
    this.setData({
      TopicContractInfo: {
        ...this.data.TopicContractInfo,
        C_Content_A: e
      }
    })
  },
  onContentChange_A(e) {
    this.setData({
      TopicContractInfo: {
        ...this.data.TopicContractInfo,
        C_Content_A: e
      }
    })
  },
  handleOk_B(date, format, e) {
    this.setData({
      TopicContractInfo: {
        ...this.data.TopicContractInfo,
        C_SignDate_B: date
      },
    })
  },
  onContractNumberChange_B(e) {
    this.setData({
      TopicContractInfo: {
        ...this.data.TopicContractInfo,
        C_ContractNumber_B: e
      }
    })
  },
  onOurCompanyChange_B(e) {
    this.setData({
      TopicContractInfo: {
        ...this.data.TopicContractInfo,
        C_OurCompany_B: e
      }
    })
  },
  onOppositeCompanyChange_B(e) {
    this.setData({
      TopicContractInfo: {
        ...this.data.TopicContractInfo,
        C_OppositeCompany_B: e
      }
    })
  },
  onContentChange_B(e) {
    this.setData({
      TopicContractInfo: {
        ...this.data.TopicContractInfo,
        C_Content_B: e
      }
    })
  },
  onContentChange_B(e) {
    this.setData({
      TopicContractInfo: {
        ...this.data.TopicContractInfo,
        C_Content_B: e
      }
    })
  },

  handlePopupClose() {
    this.setData({
      basicVisible: false,
      scrollVisible: false,
      closeVisible: false,
    });
  },
  handleShowClose() {
    this.setData({ closeVisible: true });
  },
  handleShowBasic(e) {
    var position = e.target.dataset.position;
    this.setData({
      position: position,
      basicVisible: true,
    });
    // my.getStorage({
    //   key: 'userInfo',
    //   success: (storage) => {
    //     GetTopic((res) => {
    //       const cacheData = res.data.map(it => {
    //         if (it.item1.corpId === storage.data.result.userid) {
    //           it.item1['displayAnnex'] = JSON.parse(it.item1.annex)
    //           return it
    //         }
    //       })
    //       this.setData({
    //         DisTopicData: compact(cacheData).reverse()
    //       })
    //     })
    //   }
    // })
  },
  //获取数据
  onLoad() {
    my.getStorage({
      key: 'userInfo',
      success: (storage) => {
        GetTopicContract((res) => {
          const cacheData = res.data.map(it => {
            if (it.item1.t_CorpId === storage.data.result.userid) {
              it.item1['displayAnnex'] = JSON.parse(it.item1.t_Annex)
              it.item1['displayPicture_A'] = JSON.parse(it.item1.c_Picture_A)
              it.item1['displayApprovalAnnex_A'] = JSON.parse(it.item1.c_ApprovalAnnex_A)
              it.item1['displayPicture_B'] = JSON.parse(it.item1.c_Picture_B)
              it.item1['displayApprovalAnnex_B'] = JSON.parse(it.item1.c_ApprovalAnnex_B)
              return it
            }
          })
          this.setData({
            TopicContractInfo: {
              ...this.data.TopicContractInfo,
              T_CorpId: storage.data.result.userid
            },
            ReceiveData: compact(cacheData).reverse()
          })
          console.log('ReceiveData', this.data.ReceiveData);
        })
      }
    })
  },
  // 上传数据
  onSubmit(e) {
    console.log('提交数据', this.data.TopicContractInfo);
    PostTopicContract(JSON.stringify(this.data.TopicContractInfo),
      (res) => {
        my.alert({
          title: '提交成功',
        });
        GetTopicContractById(res.data, (res) => {
          this.setData({
            ReceiveData: compact([...this.data.ReceiveData, res.data].map(it => {
              if (it.item1.t_CorpId === this.data.TopicContractInfo.T_CorpId) {
                it.item1['displayAnnex'] = JSON.parse(it.item1.t_Annex)
                it.item1['displayPicture_A'] = JSON.parse(it.item1.c_Picture_A)
                it.item1['displayApprovalAnnex_A'] = JSON.parse(it.item1.c_ApprovalAnnex_A)
                it.item1['displayPicture_B'] = JSON.parse(it.item1.c_Picture_B)
                it.item1['displayApprovalAnnex_B'] = JSON.parse(it.item1.c_ApprovalAnnex_B)
                return it
              }
            })).reverse()
          })
        })
        this.setData({
          TopicContractInfo: {
            ...this.data.TopicContractInfo,
            T_ApplicatDepart: '',
            T_TopicTitle: '',
            T_TopicContent: '',
            T_ReviewConference: '',
            T_MeetingTime: '',
            T_Participants: '武总 谢总 梁律 刘部',
            T_Annex: '[]',
            T_Notes: '',
            C_CorpId: '',
            C_ContractNumber_A: '',//合同编号
            C_SignDate_A: '',//签约日期
            C_OurCompany_A: '', //我方单位名称
            C_OppositeCompany_A: '',//对方单位名称
            C_Content_A: '',//内容
            C_Picture_A: '[]',//图片
            C_ApprovalAnnex_A: '[]', //合同审批附件
            C_ContractNumber_B: '',//合同编号
            C_SignDate_B: '',//签约日期
            C_OurCompany_B: '', //我方单位名称
            C_OppositeCompany_B: '',//对方单位名称
            C_Content_B: '',//内容
            C_Picture_B: '[]',//图片
            C_ApprovalAnnex_B: '[]', //合同审批附件
          },
        })
      },
      (err) => {
        my.alert({
          title: '提交成功1',
        });
      })
  },
  TempDataBtn(e) {
    this.setData({
      DisplayTopicContract: {
        ...this.data.DisplayTopicContract,
        T_CorpId: this.data.TopicContractInfo.T_CorpId,
        T_ApplicatDepart: this.data.TopicContractInfo.T_ApplicatDepart,
        T_TopicTitle: this.data.TopicContractInfo.T_TopicTitle,
        T_TopicContent: this.data.TopicContractInfo.T_TopicContent,
        T_ReviewConference: this.data.TopicContractInfo.T_ReviewConference,
        T_MeetingTime: this.data.TopicContractInfo.T_MeetingTime,
        T_Participants: this.data.TopicContractInfo.T_Participants,
        T_Annex: this.data.TopicContractInfo.T_Annex,
        T_Notes: this.data.TopicContractInfo.T_Notes,

      }
    })
    console.log('DisplayTopicContract', this.data.DisplayTopicContract);
  },
  onTopicUpload() {
    let imageAnnex = ''
    dd.uploadAttachmentToDingTalk({
      image: {
        max: 9,
        spaceId: SPACEID,
        compress: true,
        folderId: '',
        multiple: true,
      },
      types: ['photo'],
      success: (res) => {

        const srcAnnex = this.data.TopicContractInfo.T_Annex
        imageAnnex = res.data
        console.log('imageAnnex1', imageAnnex);
        console.log('srcAnnex', srcAnnex);
        imageAnnex.push(...JSON.parse(srcAnnex))
        console.log('imageAnnex2', imageAnnex);
        this.setData({
          displayAnnex: imageAnnex,
          TopicContractInfo: {
            ...this.data.TopicContractInfo,
            T_Annex: JSON.stringify(imageAnnex)
          }
        })
        console.log('T_Annex', this.data.TopicContractInfo);
      },
      fail: () => { },
      complete: () => { },
    });
  },
  //上传图片
  onContractUpload_A() {
    let imagePicture = ''
    dd.uploadAttachmentToDingTalk({
      image: {
        max: 9,
        spaceId: SPACEID,
        compress: true,
        folderId: '',
        multiple: true,
      },
      types: ['photo'],
      success: (res) => {
        const srcPicture = this.data.TopicContractInfo.C_Picture_A
        imagePicture = res.data
        imagePicture.push(...JSON.parse(srcPicture))
        this.setData({
          displayPicture_A: imagePicture,
          TopicContractInfo: {
            ...this.data.TopicContractInfo,
            C_Picture_A: JSON.stringify(imagePicture)
          }
        })
      },
      fail: () => { },
      complete: () => { },
    });
  },
  //上传文件
  onContractUploadFile_A() {
    let fileAnnex = ''
    dd.uploadAttachmentToDingTalk({
      types: ['file'],
      file: { spaceId: SPACEID, max: 9, folderId: '' },
      success: (res) => {
        const srcApprovalAnnex = this.data.TopicContractInfo.C_ApprovalAnnex_A
        fileAnnex = res.data
        fileAnnex.push(...JSON.parse(srcApprovalAnnex))
        this.setData({
          displayApprovalAnnex_A: fileAnnex,
          TopicContractInfo: {
            ...this.data.TopicContractInfo,
            C_ApprovalAnnex_A: JSON.stringify(fileAnnex)
          }
        })
      },
      fail: () => { },
      complete: () => { },
    });
  },
  //上传图片
  onContractUpload_B() {
    let imagePicture = ''
    dd.uploadAttachmentToDingTalk({
      image: {
        max: 9,
        spaceId: SPACEID,
        compress: true,
        folderId: '',
        multiple: true,
      },
      types: ['photo'],
      success: (res) => {
        const srcPicture = this.data.TopicContractInfo.C_Picture_B
        imagePicture = res.data
        imagePicture.push(...JSON.parse(srcPicture))
        this.setData({
          displayPicture_B: imagePicture,
          TopicContractInfo: {
            ...this.data.TopicContractInfo,
            C_Picture_B: JSON.stringify(imagePicture)
          }
        })
      },
      fail: () => { },
      complete: () => { },
    });
  },
  //上传文件
  onContractUploadFile_B() {
    let fileAnnex = ''
    dd.uploadAttachmentToDingTalk({
      types: ['file'],
      file: { spaceId: SPACEID, max: 9, folderId: '' },
      success: (res) => {
        const srcApprovalAnnex = this.data.TopicContractInfo.C_ApprovalAnnex_B
        fileAnnex = res.data
        fileAnnex.push(...JSON.parse(srcApprovalAnnex))
        this.setData({
          displayApprovalAnnex_B: fileAnnex,
          TopicContractInfo: {
            ...this.data.TopicContractInfo,
            C_ApprovalAnnex_B: JSON.stringify(fileAnnex)
          }
        })
      },
      fail: () => { },
      complete: () => { },
    });
  },
  PreviewBtn(e) {
    const item = e.currentTarget.dataset.item
    dd.previewFileInDingTalk({
      spaceId: item.spaceId,
      fileName: item.fileName,
      corpId: 'ding1b9790fbb37a3cc7f5bf40eda33b7ba0',
      fileSize: item.fileSize,
      fileType: item.fileType,
      fileId: item.fileId,
    });
  },
  CheckPictures(e) {
    const item = e.currentTarget.dataset.item
    dd.previewFileInDingTalk({
      spaceId: item.spaceId,
      fileName: item.fileName,
      corpId: 'ding1b9790fbb37a3cc7f5bf40eda33b7ba0',
      fileSize: item.fileSize,
      fileType: item.fileType,
      fileId: item.fileId,
    });
  },


});